<?php
namespace app\admin\controller;

use app\BaseController;
use app\model\SystemGroup;
use app\model\SystemInfo as SIHandle;
use app\validate\SystemInfo;
use think\exception\ValidateException;

class System extends BaseController{
    /**
     * 获取系统设置
    */
    public function get(){
        $sysHandle = new SystemGroup();
        $arr = $sysHandle->getSettingArray();
        return json(['code'=>'200','data'=>$arr,'msg' => '读取成功']);
    }
    /**
     * 修改系统设置
    */
    public function update(){
        $input = request()->put();
        // 验证数据 data 是否存在
        if(!array_key_exists('data',$input)){
            return json(['code' => 401,'msg'=> '参数错误']);
        }
        $data = $input['data'];
        // 数据完成性验证
        foreach($data as $one){
            foreach($one['children'] as $child){
                try{
                    validate(SystemInfo::class)->check($child);
                }catch(ValidateException $e){
                    return json(['code'=> 401, 'msg'=>$e->getError()]);
                }
            }
        }
        $sysHandle = new SIHandle();
        // 修改数据（最好和上面整合，但是不工整不适合我的风格）
        foreach($data as $one){
            if(!$sysHandle->updateKeyAndValue($one)){
                return json(['code'=> 500,'msg' => '数据插入出现问题!']);
            }
        }
        return json(['code'=> 200,'msg' => '数据插入成功!']);
    }
    /**
     * 恢复默认设置
    */
    public function recover(){
        $group_id = input('param.id','');
        if(!SystemGroup::where(['state'=>1,'id'=>$group_id])->count()){
            return json(['code'=> 500,'msg' => '未找到该条数据内容']);
        }
        $sysHandle = new SIHandle();
        return $sysHandle->resetting($group_id);
    }
    /**
     * 上传图片(小于两兆的图片上传)
    */
    public function upload(){
        $data = $this->uploadImage();
        return json($data);
    }
    /**
     * 文件的 断点上传
    */
    public function uploads(){
        $data = $this->uploadContinue(); // 获取上传情况
        return json($data);
    }
}